﻿using System;

namespace Dsl.Ped.Framework.Helpers 
{
    /// <summary>
    /// Helper methods for <see cref="Type"/>
    /// </summary>
    public class TypeHelper 
    {
        private static readonly Type NullAbleTypeDefinition = typeof(Nullable<>);

        /// <summary>
        /// Gets the type of the generic type
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static Type GetTypeOrNullableUnderlyingType(Type type) 
        {
            if (type.IsGenericType && type.GetGenericTypeDefinition() == NullAbleTypeDefinition) 
            {
                return Nullable.GetUnderlyingType(type);
            }

            return type;
        }
    }
}
